package io.gitee.hefren.hhsharding.demo;

import com.google.common.base.Strings;
import io.gitee.hefren.hhsharding.config.HhShardingAutoConfiguration;
import io.gitee.hefren.hhsharding.demo.dao.OrderDao;
import io.gitee.hefren.hhsharding.demo.model.Order;
import io.gitee.hefren.hhsharding.mybatis.HhShardingMapperFactoryBean;
import io.gitee.hefren.hhsharding.demo.dao.UserDao;
import io.gitee.hefren.hhsharding.demo.model.User;
import jakarta.annotation.Resource;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;

@SpringBootApplication
@MapperScan(value = "io.gitee.hefren.hhsharding.demo", factoryBean = HhShardingMapperFactoryBean.class)
@Import({HhShardingAutoConfiguration.class})
public class HhshardingApplication {

    public static void main(String[] args) {
        SpringApplication.run(HhshardingApplication.class, args);
    }

    @Resource
    private UserDao userDao;

    @Resource
    private OrderDao orderDao;

    @Bean
    public ApplicationRunner runner() {
        return args -> {
            testUser();
            testOrder();

        };
    }

    private void testUser() {
        for (int i = 0; i < 10; i++) {
            User user = new User(i, "hhsharding"+"-" + i, 15);
            // insert
            Integer insert = userDao.insert(user);
            // select
            User currentUser = userDao.findById(user.getId());
            System.out.println(Strings.lenientFormat("===> %s, insert: %s", insert, currentUser));
            // update
            user.setName(user.getName() + "-AA");
            Integer update = userDao.update(user);
            User updateUser = userDao.findById(user.getId());
            System.out.println(Strings.lenientFormat("===> %s, updateUser: %s",  update, updateUser));
            // delete
            Integer delete = userDao.delete(user.getId());
            System.out.println("===> delete: " + delete);
        }
    }

    private void testOrder() {
        for (int i = 0; i < 40; i++) {
            int uid = i<20? 0 : 1;
            Order order = new Order(i, uid, i + 0.25);
            // insert
            Integer insert = orderDao.insert(order);
            // select
            Order currentOrder = orderDao.findById(order.getId(), order.getUid());
            System.out.println(Strings.lenientFormat("===> %s, insert: %s", insert, currentOrder));
            // update
            order.setPrice(order.getPrice() + 1.2);
            Integer update = orderDao.update(order);
            Order updateOrder = orderDao.findById(order.getId(), order.getUid());
            System.out.println(Strings.lenientFormat("===> %s, updateOrder: %s",  update, updateOrder));
            // delete
            Integer delete = orderDao.delete(order.getId(), order.getUid());
            System.out.println("===> delete: " + delete);
        }
    }
}
